﻿@model EstimateShippingModel
@using Nop.Web.Models.ShoppingCart;
@if (Model.Enabled)
{
    <div class="shipping">
        <script type="text/javascript">
            seajs.use("jquery", function ($) {
                $(function () {
                    $("#@Html.FieldIdFor(model => model.CountryId)").change(function () {
                        var selectedItem = $(this).val();
                        var ddlStates = $("#@Html.FieldIdFor(model => model.StateProvinceId)");
                        var estimateProgress = $("#estimate-shipping-loading-progress");
                        estimateProgress.show();
                        $.ajax({
                            cache: false,
                            type: "GET",
                            url: "@(Url.RouteUrl("GetStatesByCountryId"))",
                            data: { "countryId": selectedItem, "addSelectStateItem": "false" },
                            success: function (data) {
                                ddlStates.html('');
                                $.each(data, function (id, option) {
                                    ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                });
                                estimateProgress.hide();
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert('Failed to retrieve states.');
                                estimateProgress.hide();
                            }
                        });
                    });
                });
            });
        </script>

        <div class="estimate-shipping">
            <div class="title">
                @T("ShoppingCart.EstimateShipping")
            </div>
            <div class="hint">@T("ShoppingCart.EstimateShipping.Tooltip")</div>
            <div class="shipping-options">
                <table>
                    <tr>
                        <td class="label">@Html.LabelFor(model => model.CountryId, new { }, ":")</td>
                        <td>
                            @Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries, new { @class = "form-control country-input" })
                            <span class="required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">@Html.LabelFor(model => model.StateProvinceId, new { }, ":")</td>
                        <td>
                            @Html.DropDownListFor(model => model.StateProvinceId, Model.AvailableStates, new { @class = "form-control state-input" })
                            <span id="estimate-shipping-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="label">@Html.LabelFor(model => model.ZipPostalCode, new { }, ":")</td>
                        <td>@Html.TextBoxFor(model => model.ZipPostalCode, new { @class = "zip-input form-control" })</td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div class="button-wrap"><input type="submit" name="estimateshipping" value="@T("ShoppingCart.EstimateShipping.Button")" class="btn btn-default estimate-shipping-button" /></div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            @if (Model.ShippingOptions.Count > 0)
                            {
                                <div class="shipping-results">
                                    @foreach (var shippingOption in Model.ShippingOptions)
                                    {
                                        <div class="shipping-option-item">
                                            <div class="option-name">
                                                @T("ShoppingCart.EstimateShipping.ShippingOptionWithRate", shippingOption.Name, shippingOption.Price)
                                            </div>
                                            <div class="option-description">
                                                @Html.Raw(shippingOption.Description)
                                            </div>
                                        </div>
                                    }
                                </div>
                            }
                        </td>
                    </tr>
                    @if (Model.Warnings.Count > 0)
                    {
                        <tr class="message-error">
                            <td colspan="2">
                                <ul>
                                    @foreach (var error in Model.Warnings)
                                    {
                                        <li>@error</li>
                                    }
                                </ul>
                            </td>
                        </tr>
                    }
                </table>
            </div>
        </div>
    </div>
}